/** 三国战记风云再起
  
  By setycyas @2024-03-12
*/

/* 全局设定
*/
#Warn
#MaxThreadsPerHotkey 1
SetWorkingDir(A_ScriptDir)
;导入自定义函数库
#Include "..\Class\OrderMap.2.ahk"
#Include "..\Class\MyFuns.2.ahk"
;导入宏相关函数和UI
#Include "..\Class\FaceDetector.2.ahk"
#Include "..\Class\CommonMacro.2.ahk"
#Include "..\Class\CommonUI.2.ahk"
#Include "..\Class\MyEmu.2.ahk"

SendMode("Input")

/* 制作三国战记风云再起的宏类
*/
class KovSHMacro extends CommonMacro {

  pressDuration := 40

  /* 技能函数 
  */
  
  /* 宏函数
  */
  ;连发需要改装,保证第一下的延迟,否则前冲A容易打不出来!
  _连发A(key){
    local vk
    this._press(1)
    if (this.autoFireDelay == 0){ ;单发
      sleep(100)
      return
    } else {
      vk := GetKeyVk(MyFuns.getHotkey(key))
      loop 3 { ;增加第一发延迟
        if (MyFuns.getVkState(vk)){
          sleep(40)
        } else {
          return
        }
      }
      ; 还按住键则连发,否则停顿一下再中止避免按键太快重新被触发
      if ((!MyFuns.getVkState(vk))){
        sleep(50)
        return
      } else {
        ;继续连发
        super._连发A(key)
      }
    }
  }
  ;连发前一个C保证蹲下后站起,实测表明配合蹲A脚本用于蹲连可行!
  _连发CA(key){
    local vk
    this._down(3)
    this._press(1)
    this._up(3)
    sleep(50)
    vk := GetKeyVk(MyFuns.getHotkey(key))
    if (MyFuns.getVkState(vk)){
      super._连发A(key)
    }else{
      sleep(100)
    }
  }
  ;兼容连击
  _前A(key){
    local face := this._face.face()
    local vk := GetKeyVk(MyFuns.getHotkey(key))
    MyFuns.pressKeys([face, this._keys[1]], [30, 30])
    sleep(50)
    if(MyFuns.getVkState(vk)){
      super._连发A(key)
    }
  }
  _蹲A(key){
    MyFuns.keyDown("down")
    sleep(30)
    this._down(2)
    sleep(30)
    this._up(2)
    MyFuns.keyUp("down")
    sleep(30)
    this._press(1)
    sleep(100)
  }
  ;使用方法是连发a之后按住这个快捷键,自动蹲a和CA连发站起攻击
  _蹲连(key){
    MyFuns.keyDown("down")
    sleep(30)
    this._down(2)
    sleep(30)
    this._up(2)
    MyFuns.keyUp("down")
    sleep(30)
    this._press(1)
    sleep(500) ;重要参数
    if (MyFuns.GetKeyState(key)){
      this._连发CA(key)
    }
  }
  ;经过搜索,发现原来键盘的跑动有时会失败,需要连按三下方向才能保证成功!
  _跑动(key){
    loop 3 {
      this._faceDown()
      sleep(30) 
      this._faceUp()
      sleep(30)
    }
  }
  _下前A(key){
    local face := this._face.face()
    MyFuns.keyDown("down")
    sleep(30)
    MyFuns.keyDown(face)
    sleep(30)
    MyFuns.keyUp("down")
    sleep(30)
    this._down(1)
    sleep(30)
    MyFuns.keyUp(face)
    sleep(30)
    this._up(1)
  }
  ;放手时再次输入a扔出长矛
  _下前A_赵云专用(key){
    static DELAY1 := 600 ;最大放手延迟,超过这个数值不再输入a
    local face := this._face.face()
    local vk ;为保证效率,等一下再赋值
    MyFuns.keyDown("down")
    sleep(30)
    MyFuns.keyDown(face)
    sleep(30)
    MyFuns.keyUp("down")
    sleep(30)
    this._down(1)
    sleep(30)
    MyFuns.keyUp(face)
    sleep(30)
    this._up(1)
    ;等待DELAY1,如放手则直接输入a,否则不再输入并停一下避免重复本热键
    vk := GetKeyVk(MyFuns.getHotkey(key))
    if (MyFuns.waitVkUp(vk, DELAY1, 40)){
      this._press(1)
    }else{
      sleep(500)
    }
  }
  _后前A(key){
    local face := this._face.face()
    local back := this._face.back()
    MyFuns.press(back, 50)
    sleep(50)
    MyFuns.pressKeys([face, this._keys[1]], [30, 30])
    sleep(600)
  }
  _下上A(key){
    MyFuns.press("down", 50)
    sleep(50)
    MyFuns.pressKeys(["up", this._keys[1]], [30, 30])
  }
  _上下A(key){
    MyFuns.press("up", 50)
    sleep(30)
    MyFuns.keyDown("down")
    sleep(30)
    this._down(1)
    sleep(30)
    MyFuns.keyUp("down")
    sleep(30)
    this._up(1)
  }
  _下下A(key){
    send("{down down}")
    sleep(30)
    send("{down up}")
    ;sleep(50)
    sleep(30)
    send("{down down}")
    ;sleep(60)
    sleep(30)
    send("{down up}")
    this._press(1)
    sleep(100)
  }
  ;放手时才松开,兼容赵云超必杀,貂蝉的连击另外设定专用版
  _上下A_赵云专用(key){
    local vk ;为保证效率,等一下再赋值
    MyFuns.press("up", 50)
    sleep(30)
    MyFuns.keyDown("down")
    sleep(30)
    this._down(1)
    sleep(30)
    MyFuns.keyUp("down")
    vk := GetKeyVk(MyFuns.getHotkey(key))
    loop {
      sleep(50)
    } until (!MyFuns.getVkState(vk))
    this._up(1)
  }
  ;貂蝉纵云踢
  _上下A_貂蝉专用(key){
    local vk ;为保证效率,等一下再赋值
    MyFuns.press("up", 50)
    sleep(30)
    MyFuns.keyDown("down")
    sleep(30)
    this._press(1)
    sleep(30)
    MyFuns.keyUp("down")
    sleep(100)
    vk := GetKeyVk(MyFuns.getHotkey(key))
    while(MyFuns.getVkState(vk)){
      this._press(1)
      sleep(70)
    }
  }
  ;追地用
  _追地后前A(key){
    this._down(1)
    sleep(30)
    this._up(1)
    sleep(150)
    this._后前A(key)
  }
  _前前A(key){
    local face := this._face.face()
    local faceDown := "{" . face . " down}" ;为保证速度手动处理
    local faceUp := "{" . face . " up}"
    ;跑动3次
    send(faceDown)
    sleep(30)
    send(faceUp)
    sleep(30)
    send(faceDown)
    sleep(30)
    send(faceUp)
    sleep(30)
    send(faceDown)
    sleep(30)
    send(faceUp)
    sleep(30) ;需要测试的关键参数1
    this._press(1)
  }
  ;反身冲,不按攻击键
  _反身冲(key){
    loop 2 {
      this._backDown()
      sleep(30)
      this._backUp()
      sleep(30)
      this._faceDown()
      sleep(30)
      this._faceUp()
      sleep(30)
    }
    ;this._press(1)
  }
  ;撞连,平a结束后手动换到这个键,跟蹲连一样.
  ;主要是张飞和黄忠,测试过参数可行,风云效果更佳10级即可顺利打出!
  _撞连(key){
    this._跑动(key)
    this._press(1)
    sleep(100) ;需要测试的关键参数
    if (MyFuns.GetKeyState(key)){
      super._连发A(key)
    }
  }
  ;爆气与爆气时出招,主要是要兼容关羽和赵云的爆气时打浮空
  _爆气(key){
    this._down(5)
    sleep(30)
    this._up(5)
  }
  _爆气前A(key){
    this._爆气(key)
    sleep(50)
    this._前A(key)
  }
  _爆气前A连打(key){
    this._爆气(key)
    sleep(50)
    this._前A(key)
    loop 10 {
      this._down(1)
      sleep(40)
      this._up(1)
      sleep(40)
    }
  }
  ;这招可行,打完微停接下上A即可,再后面不好接的话立即接超必杀
  ;因为这一招打飞敌人不多,反而容易控制,默认用这招.
  _爆气下前A_赵云专用(key){
    this._爆气(key)
    sleep(50)
    this._下前A(key)
    sleep(300)
    this._press(1)
  }
  _爆气下上A(key){
    this._爆气(key)
    sleep(50)
    this._下上A(key)
  }
  ;魔张的两招连接,可中断
  _后前A下前A(key){
    this._后前A(key)
    ;中断判断
    if (MyFuns.waitKeyUp(key, 400)){
      return
    } else {
      this._下前A(key)
      sleep(200)
    }
  }
  ;跳起后立即下A,适用于张辽和白黄的空中最低点攻击
  _空中快速下A(key){
    this._press(2)
    sleep(30)
    MyFuns.keyDown("down")
    sleep(30)
    this._press(1)
    sleep(30)
    MyFuns.keyUp("down")
  }
  ;张飞张辽的爆气连击,原本打算打许褚用,发现不如不爆气直接打...
  _A下前A(key){
    this._press(1)
    sleep(70)
    this._下前A(key)
  }
  /* 风云再起专门招 */
  ;马超赵云的空中上3A
  _空中上3A(key){
    this._press(2)
    sleep(50) ;测试参数1
    MyFuns.keyDown("up")
    this._press(1)
    MyFuns.keyUp("up")
    loop 2 {
      sleep(150) ;测试参数2
      this._press(1)
    }
  }
  ;赵云的空中上2A+取消,第二下A后立即再按A产生取消效果,所以其实是3次A
  ;只有空中指令没有跳指令,因为出招时机需要即时判断
  ;参数还行,操作太难,放弃...
  _赵云空消3A(key){
    MyFuns.keyDown("up")
    this._press(1)
    sleep(this.testParams[1]) ;测试参数1,400
    this._press(1)
    sleep(this.testParams[2]) ;测试参数2,30
    this._press(1)
    MyFuns.keyUp("up")
  }
  ;张辽前A3击,只打三下
  _张辽前A3击(key){
    this._前A(key)
    sleep(520) ;this.testParams[1]
    this._press(1)
  }
  ;下前B滑铲
  _滑铲(key){
    local face := this._face.face()
    MyFuns.keyDown("down")
    sleep(30)
    MyFuns.keyDown(face)
    sleep(30)
    MyFuns.keyUp("down")
    sleep(30)
    this._down(2)
    sleep(30)
    MyFuns.keyUp(face)
    sleep(30)
    this._up(2)
  }

  /* 重载宏接口
  */
  _setMacro(){
    static obm := ObjBindMethod 
    this.macroMap := OrderMap()
    this.macroMap["连发A"] := obm(this, "_连发A")
    this.macroMap["连发CA"] := obm(this, "_连发CA")
    this.macroMap["前A"] := obm(this, "_前A")
    this.macroMap["蹲A"] := obm(this, "_蹲A")
    this.macroMap["蹲连"] := obm(this, "_蹲连")
    this.macroMap["跑动"] := obm(this, "_跑动")
    this.macroMap["下前A"] := obm(this, "_下前A")
    this.macroMap["下前A_赵云专用"] := obm(this, "_下前A_赵云专用")
    this.macroMap["后前A"] := obm(this, "_后前A")
    this.macroMap["下上A"] := obm(this, "_下上A")
    this.macroMap["上下A"] := obm(this, "_上下A")
    this.macroMap["下下A"] := obm(this, "_下下A")
    this.macroMap["上下A_赵云专用"] := obm(this, "_上下A_赵云专用")
    this.macroMap["上下A_貂蝉专用"] := obm(this, "_上下A_貂蝉专用")
    this.macroMap["追地后前A"] := obm(this, "_追地后前A")
    this.macroMap["前前A"] := obm(this, "_前前A")
    this.macroMap["反身冲"] := obm(this, "_反身冲")
    this.macroMap["撞连"] := obm(this, "_撞连")
    this.macroMap["爆气"] := obm(this, "_爆气")
    this.macroMap["爆气前A"] := obm(this, "_爆气前A")
    this.macroMap["爆气前A连打"] := obm(this, "_爆气前A连打")
    this.macroMap["爆气下上A"] := obm(this, "_爆气下上A")
    this.macroMap["爆气下前A_赵云专用"] := obm(this, "_爆气下前A_赵云专用")
    this.macroMap["后前A下前A"] := obm(this, "_后前A下前A")
    this.macroMap["空中快速下A"] := obm(this, "_空中快速下A")
    this.macroMap["A下前A"] := obm(this, "_A下前A")
    this.macroMap["滑铲"] := obm(this, "_滑铲")
    this.macroMap["空中上3A"] := obm(this, "_空中上3A")
    this.macroMap["赵云空消3A"] := obm(this, "_赵云空消3A")
    this.macroMap["张辽前A3击"] := obm(this, "_张辽前A3击")
    this.macroArr := this.macroMap.getKeys()
  }

}

/* 制作三国战记风云再起专用UI
*/
class KovSHUI extends CommonUI {

  DEFAULT_HOTKEYS := "~d,~s,~e,~r,~g,~w"
  EXE_ARRAY := [
    MyEmu.FBAS, MyEmu.XZONE
  ]

  /* 构造函数
  */
  __New(macro){
    super.__New("三国战记风云再起", macro)
    this._preset := OrderMap()
    ;测试过效果尚可的人物
    this._preset["白黄忠"] := ["连发A", "反身冲", "下前A", "上下A", "蹲连", "爆气"]
    this._preset["真黄忠"] := ["连发A", "反身冲", "后前A", "上下A", "蹲连", "爆气"]
    this._preset["诸葛亮"] := ["连发A", "反身冲", "下前A", "下上A", "蹲连", "爆气"]
    this._preset["马超"] := ["连发A", "上下A", "空中上3A", "下上A", "蹲连", "爆气"]
    this._preset["张辽"] := ["连发A", "张辽前A3击", "后前A", "下上A", "蹲连", "爆气"]
    ;从119搬过来或测试效果尚不佳的人物
    this._preset["白张飞"] := ["连发A", "后前A", "下前A", "下上A", "下下A", "爆气"] ;等级到10可以用撞连
    this._preset["魔张飞"] := ["连发A", "后前A", "下前A", "下下A", "后前A下前A", "爆气"] ;等级到10可以用撞连
     ;等级到10可以用撞连
    this._preset["赵云"] := ["连发A", "下上A", "赵云空消3A", "上下A_赵云专用", "滑铲", "爆气下前A_赵云专用"]
    
    
    this._preset["关羽"] := ["连发A", "下前A", "前A", "下上A", "跑动", "爆气前A"]
    
    this._preset["貂蝉"] := ["连发A", "下前A", "上下A_貂蝉专用", 
      "下上A", "蹲A", "爆气"]
  }

  /* 重写接口
  */
  ;首次提交数据,暂时没有准备操作
  /*
  _onSubmitEnd(){
    super._onSubmitEnd()
    if (this._submitTimes == 0){
      HotIfWinActive(this._winTitle)
      Hotkey("RCtrl", this._changeCheatWeapon.bind(this))
      HotIfWinActive()
    }
  }
  */

  /* 重写setup,更改可选的exe,读入预设
  */
  setup(){
    super.setup()
    this._myGui.opt("+AlwaysOnTop")
    this._myGui.show("x1100 y330")
    this.readPresetMenu("白张飞")
  }

}

/* 运行当前脚本
*/
if (A_ScriptName == "三国战记风云再起.2.ahk"){
  face := FaceDetector() ;方向检测对象
  macro := KovSHMacro(face, ["q", "f", "v", "c", "x"]) ;宏对象,有界面时第二个参数可以随便写但长度不要小于2
  ;新建界面并设定
  ui := KovSHUI(macro)
  ui.setup()
  ui._exeComboBox.Choose(MyEmu.FBAS)
  ui._gameKeyEdit.Text := "q,f,v,c,x" ;最后一个是爆气键
  ;ui._hotkeyEdit.Text := "~d,s,c,e,r,g"
  ui._fpsEdit.Text := 13
  ui.readPresetMenu("白张飞")
}